﻿var Utils = {
    defaultEmployeePhotoPath: function(cnp) {
        var sex = Utils.getUserSex(cnp);
        return sex == "M" ? getImageUrl('generic-user-male.jpg') : getImageUrl('generic-user-female.jpg');
    },
    defaultCompanyPhotoPath: getImageUrl('generic-company.png'),
    getEmployeePhoto: function(prsId, prsCnp) {
        if (!prsId || !app.TMMData || !app.TMMData.SandboxPath || !app.User.POP_ID)
            return Utils.defaultEmployeePhotoPath(prsCnp);

        return app.TMMData.SandboxPath + app.User.POP_ID + '/' + prsId + '.jpg';
    },
    initializeDatePicker: function(input, onSelectCallback, preset) {
        var culture = Globalize.culture();
        var dateFormat = culture.calendar.patterns.d.replaceAll(culture.calendar["/"], ' ');

        input.scroller({
            preset: preset || 'date',
            stepMinute: 5,
            timeWheels: 'HHii',
            ampm: false,
            theme: 'android-ics light',
            display: 'modal',
            mode: 'mixed',
            dateOrder: culture.calendar.patterns.d,
            headerText: false,
            dateFormat: dateFormat,
            timeFormat: 'HH:ii',
            dayNames: culture.calendar.days.names,
            dayNamesShort: culture.calendar.days.namesShort,
            dayText: translate('General_Zi'),
            monthNames: culture.calendar.months.names,
            monthNamesShort: culture.calendar.months.namesAbbr,
            monthText: translate('General_Luna'),
            yearText: translate('General_An'),
            setText: translate('General_Setare'),
            cancelText: translate('General_Renuntare'),
            onSelect: function() {
                if (onSelectCallback)
                    onSelectCallback();
            },
            onShow: function() {
                $(document).trigger('overlayOpened');
                input.attr('visible', 'visible');
            },
            onClose: function() {
                $(document).trigger('overlayClosed');
                input.removeAttr('visible');
            },
            onCancel: function() {
            }
        });
    },
    getDateDiff: function(d1, d2) {
        var oneDay = 1000 * 60 * 60 * 24;
        return Math.floor((Math.abs(d1.getTime() - d2.getTime())) / oneDay) + 1;
    },
    getBlackberryVersion: function() {
        var isBlackBerry = /blackberry/i.test(navigator.userAgent.toLowerCase());
        if (!isBlackBerry)
            return 0;

        var ua = navigator.userAgent;
        if (ua.indexOf("BlackBerry") >= 0) {
            if (ua.indexOf("Version/") >= 0) {
                var verposition = ua.indexOf("Version/") + 8;
                return ua.substring(verposition, verposition + 3);
            } else {
                var splitUa = ua.split("/");
                return splitUa[1].substring(0, 3);
            }
        }

        return 0;
    },
    scrollParentIntoView: function(elem) {
        setTimeout(function() {
            var children = elem.parentNode.children;
            var lastChild = children[children.length - 1];
            lastChild.scrollIntoView();
            elem.parentNode.scrollIntoView();
        }, 1);
    },
    getFileExtension: function(filename) {
        return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
    },
    getTimeDifferenceText: function(ds, de) {
        if (!ds || !de)
            return "";

        var timeDiff = Time.getTimeDifference(ds, de);

        var text = "";
        if (timeDiff.H || (!timeDiff.H && !timeDiff.M))
            text += timeDiff.H + " " + (timeDiff.H == 1 ? translate('General_Ora') : translate('General_Ore'));
        if (timeDiff.H && timeDiff.M)
            text += " " + translate('General_Si') + " ";
        if (timeDiff.M)
            text += timeDiff.M + " " + (timeDiff.M == 1 ? translate('General_Minut') : translate('General_Minute'));
        return text;
    },
    getQueryString: function(ji) {
        var hu = window.location.search.substring(1);
        var gy = hu.split("&");
        for (var i = 0; i < gy.length; i++) {
            var ft = gy[i].split("=");
            if (ft[0] == ji)
                return ft[1];
        }
    },
    getUserSex: function(cnp) {
        cnp = ((cnp || (app.User ? app.User.CNP : "")) || "").toString();

        var firstLetter = cnp.substr(0, 1);
        var isFemale = cnp && (firstLetter == 2 || firstLetter == 4 || firstLetter == 6);

        return isFemale ? "F" : "M";
    },
    getEmptyGuid: function() {
        return "00000000-0000-0000-0000-000000000000";
    }
};